jconed.toolbar.register_handler ( 'Image', function ( editor )
{
	var f = new liwe.form.instance ( 'jconed-image', 'no-action' );
	f.text ( { label: 'Image URL', name: 'image', size: 30, nonl: true } );
	/* versione download_manager 
	if ( typeof ( download_manager ) != 'undefined' )
		f.button ( { value: '...', onclick: 'jconed.__image_dlm(); return false' } );
	*/
	/* versione media_manager */
	if ( typeof ( media_manager ) != 'undefined' )
		f.button ( { value: '...', onclick: 'jconed.__image_mm(); return false' } );
	f.submit ( 'Insert' );
	f.events [ 'submit' ] = function ()
	{
		var image = f.get_value ( 'image' );
		jconed.execute ( 'insertImage', image );
		return false;
	};
	jconed.toolbar.set_context ( f.get () );
} );

jconed.register_context_handler ( function ( editor )
{
	var sel = jconed.editor.get_selection () [ 0 ];
	var img = sel.startContainer.childNodes [ sel.startOffset ];
	if ( ! img || img.tagName != 'IMG' ) return;

	var w = parseInt ( img.width || img.style.width, 10 );
	var h = parseInt ( img.height || img.style.height, 10 );
	var hs = parseInt ( img.hspace, 10 );
	var vs = parseInt ( img.vspace, 10 );

	var buttons = '' +
		'<div class="jconed-button jconed-image-float-none" onclick="jconed.__image_context(\'none\')"></div>' +
		'<div class="jconed-button jconed-image-float-left" onclick="jconed.__image_context(\'left\')"></div>' +
		'<div class="jconed-button jconed-image-float-right" onclick="jconed.__image_context(\'right\')"></div>'; 
	var f = new liwe.form.instance ( 'jconed-image', 'no-action' );
	f.number ( { label: 'Width', name: 'width', size: 5, value: w, onkeyup: 'jconed.__image_context()', nonl: true } );
	f.number ( { label: 'Height', name: 'height', size: 5, value: h, onkeyup: 'jconed.__image_context()' } );
	f.number ( { label: 'HSpace', name: 'hspace', size: 5, value: hs, onkeyup: 'jconed.__image_context()', nonl: true } );
	f.number ( { label: 'VSpace', name: 'vspace', size: 5, value: vs, onkeyup: 'jconed.__image_context()' } );
	f.descr ( { label: 'Alignment', text: buttons } );
	jconed.toolbar.set_context ( f.get () );
} );

jconed.__image_context = function ( align )
{
	var sel = jconed.editor.get_selection () [ 0 ];
	var img = sel.startContainer.childNodes [ sel.startOffset ];
	if ( align )
	{
		img.style.cssFloat = align;
		img.style.styleFloat = align;
	}
	else
	{
		var f = liwe.form.get ( 'jconed-image' ); 
		var w = f.get_value ( 'width' );
		var h = f.get_value ( 'height' );
		var hs = f.get_value ( 'hspace' );
		var vs = f.get_value ( 'vspace' );
		img.style.width = ( w ? w + 'px' : '' );
		img.style.height = ( h ? h + 'px' : '' );
		img.hspace = hs;
		img.vspace = vs;
	}
};

jconed.__image_dlm = function ()
{
	liwe.lightbox.fade = false;
	liwe.lightbox.easy ( "jconed_image", "Inserimento immagine", 'auto', 'auto' );
	download_manager.cbacks [ 'image_click' ] = function ( url )
	{
		var f = liwe.form.get ( 'jconed-image' );
		f.set_value ( 'image', url );
		liwe.lightbox.close ();
		return false;
	};
	download_manager.render ( "jconed_image" );
};

jconed.__image_mm = function ()
{
	liwe.lightbox.fade = false;
	//if ( ! liwe.lightbox.created () )
	if ( ! $( 'jconed_image_lb' ) )
		liwe.lightbox.easy ( "jconed_image_lb", "Inserimento immagine", 660, 500 );

	//default values
	var _jmodule = 'jconed', _jid_obj = 0, chk_fields = false;
	var f, form_name, force = true, order_by = 'data desc';
	var params = jconed.editor.current.get ( 'params' );
	var up_form, icons = {}, site_module = null;
	var items_per_row = 5;
	var templates = {
				'icon' : 	'<div><img src="/site/media_manager/image/icon/%(id_media)s.jpg" alt="" border="0" /></div>' +
						'<div><span><button onclick="jconed.__img_del_item ( \'%(id_media)s\', \'%(module)s\' );" class="red">&nbsp;Delete&nbsp;</button></span>' +
						'<span><button onclick="media_manager.admin._set_form_url ( \'%(id_media)s\', \'icon\' );">&nbsp;icon&nbsp;</button></span></div>' +
						'<div><span><button onclick="media_manager.admin._set_form_url ( \'%(id_media)s\', \'small\' );">&nbsp;small&nbsp;</button></span>' +
						'<span><button onclick="media_manager.admin._set_form_url ( \'%(id_media)s\', \'thumb\' );">&nbsp;thumb&nbsp;</button></span></div>' +
						'<div><span><button onclick="media_manager.admin._set_form_url ( \'%(id_media)s\', \'full\' );">&nbsp;full&nbsp;</button></span>' +
						'<span><button onclick="media_manager.admin._set_form_url ( \'%(id_media)s\', \'orig\' );">&nbsp;orig&nbsp;</button></span></div>',  
				'ui'	: 	'<div class="title">Upload immagine</div><div id="jconed-upl-form"></div> <div class="title">Immagini non in gallery</div><div>%(_no_gallery)s</div> <div class="title">Immagini in gallery</div><div>%(_gallery)s</div>' 
			};
	//check if the form has jconed specific fields
	if ( params && params.get ( 'form_inst' ) )
	{
		form_name = params [ 'form_inst' ][ 'name' ];
		f = liwe.form.get ( form_name );
		module = f.get_value ( 'jconed_module' );
		id_obj = f.get_value ( 'jconed_id' );
		if ( module && id_obj ) chk_fields = true;
	}
	//warn in case they are missing
	//if ( ! chk_fields )
		//console.warn ( "\nJCONED images plug-in: jconed_module and jconed_id form fields are missing !! " );

	//get items list of jconed images
	media_manager.get_items ( _jmodule, _jid_obj, function ( items_jconed )
		{
			media_manager.admin.items = items_jconed;
			//media_manager.admin.list_items (); 
			icons [ 'no_gal' ] = media_manager._render_icons ( items_jconed, 'icon', templates [ 'icon' ], items_per_row );

			function _render_mm_items () {
				//render jconed plugin layout
				$( "jconed_image_lb" , String.formatDict ( templates [ 'ui' ], { '_gallery' : icons [ 'gal' ], '_no_gallery' : icons [ 'no_gal' ] } ) );
				//call upload form function and render form
				jconed.__img_upload_form ( 'jconed-upl-form', _jid_obj, _jmodule );
				jconed.__img_show_more ();
			}

			if ( ! chk_fields ) {
				_render_mm_items ();
				return;
			}

			//get list of media_manager images
			media_manager.get_items ( module, id_obj, function ( items )
				{
					media_manager.admin.items = items;
					//media_manager.admin.list_items (); 
					icons [ 'gal' ] = media_manager._render_icons ( items, 'icon', templates [ 'icon' ], items_per_row );
					_render_mm_items ();
				}, media_manager.admin.templates, force );

		}, media_manager.admin.templates, force, order_by ); 

};

jconed.__img_upload_form = function ( dest_div, id_obj, module )
{
        var f = new liwe.form.instance ( "mm-admin-edit" );
        var item, action, title, kind = 'image';

        f.events [ 'complete' ] = "jconed.__image_mm";
        item = { "data" : { "id_media" : 0, "kind" : kind } };
        f.hidden ( "action", "media_manager.admin.add" );
        f.hidden ( "id_obj", id_obj );
        f.hidden ( "module", module );
        f.hidden ( "kind", kind );

        f.text ( { label: "Descr", name: "descr", value: item.data [ 'descr' ], size: 40, maxlength: 200 } );
        f.hidden ( "__PYHP_SESSION", liwe.cookie.get ( "session" ) );
	f.uploader ( { label: "URL", name: "data", type: 0 } );
        f.submit ( { value: "Invia" } );
        f.set ( dest_div );
};

jconed.__img_del_item = function ( id_media, module )
{
        if ( ! confirm ( "Confermi la cancellazione di questo elemento?" ) ) return;

        media_manager.admin.ajax ( { action: "media_manager.admin.del_media", id_media: id_media }, function ( v )
                {
                        media_manager.admin._update_items ( v.media_items );
			jconed.__image_mm ();
		} );
};

jconed.__img_show_more = function ( init )
{
	function _set ()
	{
		var c, ff = liwe.form.get ( 'jconed_show_more' );
		var ss = ff.get_value ( 'f_state' );
		var func = liwe.dom.hide;
		if ( ss != 'hidden' )
			func = function ( el ) { el.style.display = ''; };

		for ( c = num_rows; c < len; c++ )
		{
			func ( tr [ c ] );
		};
	};

	var num_rows = 2, cnt = $( 'jconed_image_lb' );
	if ( ! cnt ) return;
	var tbl = $c( 'table', 'media_manager_icons', cnt );
	if ( tbl.length < 1 ) return;
	var tr = tbl [ 0 ].getElementsByTagName ( 'tr' );
	var len = tr.length;

	jconed.__img_results_butt ( tbl [ 0 ] );
	_set ();
};

jconed.__img_results_butt = function ( el )
{
	var msg, val, s, f = liwe.form.get ( 'jconed_show_more' );
	if ( f )
	{
		s = f.get_value ( 'f_state' );
		val = ( s == 'hidden' ) ? 'all' : 'hidden';
		msg = ( s == 'hidden' ) ? 'Nascondi alcuni risultati' : 'Mostra tutti i risultati';
		f.set_value ( 'f_state', val );
		//f.set_value ( 'show_butt', msg );
		$( 'jconed_show_more@show_butt', msg );
	} else {
		f = new liwe.form.instance ( 'jconed_show_more' );
		f.hidden ( { 'name': 'f_state', 'value' : 'hidden' } );
		f.button ( { 'name' : 'show_butt', 'value' : 'Mostra tutti i risultati', 'class' : 'green', onclick:"jconed.__img_show_more ();" } );
	}
	if ( $( 'jconed_show_more' ) ) return;
	var butt = document.createElement ( 'div' );
	butt.innerHTML = f.get ();
	el.parentNode.appendChild ( butt );
};
